﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using InterfaceComponents;

namespace DrawableObjects.Actors.Aircrafts.Controller
{
    /// <summary>
    /// A controller for a drone in the game. Is used to asign an aircraft to a mothership.
    /// Depending on the drone mode, orders the aicraft to execute certain command patterns
    /// </summary>
    public class AIControlledAircraft_Drone : AircraftController
    {
        private Aircraft ownerShip
        {
            get;
            set;
        }


        private float circle = 0;

        private float posDef = 0;

        private const int RADIUS = 35;

        public HUDWrapper<DroneMode> mode
        {
            get;
            private set;
        }

        // the pervious droneMode
        private DroneMode oldMode;

        //Upgrades all of the mothership's weapons of the given type
        public void UpgradeOwnerShip(Weapons.Weapon.WeaponType type) 
        {
            ownerShip.Upgrade(type);
        }

        public override void Update()
        {
            Update(mode.value);
        }

        public void Update(DroneMode mode)
        {
            if (mode != oldMode)
            {
                if (mode == DroneMode.DEFEND)
                {
                    aircraft.aircraftAttributes.Remove(Aircraft.AircraftAttributes.Transparent_Projectile);
                }
                else 
                {
                    aircraft.aircraftAttributes.Add(Aircraft.AircraftAttributes.Transparent_Projectile);
                }

                oldMode = mode;
            }
            if (mode != null)
            {
                this.mode.value = mode;
            }
            if(this.mode.value != null) {
                if (this.mode.value.standardKey.Equals(DroneMode.ATTACK.standardKey))
                {   executeAttackMode();
                } else if (this.mode.value.standardKey.Equals(DroneMode.DEFEND.standardKey)) {
                    executeDefendMode();
                } else if (this.mode.value.standardKey.Equals(DroneMode.REPAIR.standardKey)) {
                    executeRepairMode();
                } else if (this.mode.value.standardKey.Equals(DroneMode.COLLECT.standardKey)) {
                    executeCollectMode();
                }
            }
        }


        public void executeAttackMode() {
            Vector2 shipPosition = ownerShip.getGamePosition();

            float posX = ((float)Math.Sin(MathHelper.ToRadians(circle))) * RADIUS;
            float posY = ((float)Math.Cos(MathHelper.ToRadians(circle))) * RADIUS;

            Vector2 targetPosition = new Vector2(shipPosition.X + posX, shipPosition.Y + posY);

            aircraft.moveTowards(targetPosition);
            aircraft.Fire(0, new Vector2(0,-2));

            if ((Vector2.Subtract(targetPosition, aircraft.getGamePosition()).Length()) < 20.0f) {
                circle += 1.2f;
                circle %= 360f;
            }
        }

        public void executeDefendMode() {
            Vector2 shipPosition = ownerShip.getGamePosition();

            Vector2 targetPosition = new Vector2(shipPosition.X + posDef, shipPosition.Y - 35);

            aircraft.moveTowards(targetPosition);
            if ((Vector2.Subtract(targetPosition, aircraft.getGamePosition()).Length()) < 20.0f) {
                if (Math.Abs(posDef) >= 40) {
                    if (posDef > 0) {
                        posDef -= 5;
                    } else {
                        posDef += 5;
                    }
                }
                if (Math.Abs(posDef % 2) == 1) {
                    posDef -= 4;
                } else {
                    posDef += 4;
                }
            }
        }

        public void executeRepairMode() {
            Vector2 shipPosition = ownerShip.getGamePosition();

            Vector2 targetPosition = new Vector2(shipPosition.X, shipPosition.Y + 45);
            aircraft.moveTowards(targetPosition);

            if ((Vector2.Subtract(targetPosition, aircraft.getGamePosition()).Length()) < 60.0f) {

                aircraft.Fire(1, new Vector2(0,-4), Owner.ENEMY);
            }
        }

        public void executeCollectMode() {
            Item item = (Item) aircraft.world.GetClosestElement(aircraft.getGamePosition(), typeof(Item));

            if (item != null) {
                Vector2 itemPos = item.getGamePosition();
                aircraft.moveTowards(itemPos);
            } else {
                Vector2 targetPos = ownerShip.getGamePosition();
                targetPos.Y += 30;
                aircraft.moveTowards(targetPos);
            }
        }

        public AIControlledAircraft_Drone(Aircraft ownerShip, Aircraft aircraft)
        {
            this.ownerShip = ownerShip;
            this.aircraft = aircraft;
            mode = new HUDWrapper<DroneMode>(new List<HUDComponent>());
            mode.value = DroneMode.ATTACK;
            aircraft.aircraftAttributes.Add(Aircraft.AircraftAttributes.Transparent_Projectile);
        }

    }
}

